package com.ewhale.feitengguest.ui.shop.msg

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.ShopMessageDto
import com.ewhale.feitengguest.dto.TabEntity
import com.ewhale.feitengguest.presenter.shop.message.SMessagePresenter
import com.ewhale.feitengguest.presenter.shop.task.STaskPresenter
import com.ewhale.feitengguest.ui.WebViewActivity
import com.ewhale.feitengguest.ui.shop.msg.adapter.SMessageAdapter
import com.ewhale.feitengguest.ui.shop.task.STaskDetailActivity
import com.ewhale.feitengguest.view.shop.message.SMessageView
import com.ewhale.feitengguest.view.shop.task.STaskView
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.simga.library.activity.MBaseFragment
import com.simga.library.utils.Dp2PxUtil
import com.simga.library.widget.RecycleViewDivider
import kotlinx.android.synthetic.main.fragment_shop_message.*
import java.util.ArrayList

class SMessageFragment : MBaseFragment<SMessagePresenter>(), SMessageView {

    override fun getViewId(): Int {
        return R.layout.fragment_shop_message
    }

    private val mTabEntities = ArrayList<CustomTabEntity>()
    private var pageNumber = 1
    private val mAdapter by lazy { SMessageAdapter() }
    override fun init(savedInstanceState: Bundle?) {
        mTabEntities.add(TabEntity("系统消息", 0, 0))
        mTabEntities.add(TabEntity("用户留言", 0, 0))
        topMainTab.setTabData(mTabEntities)
        list.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
            addItemDecoration(RecycleViewDivider(mContext,
                    Dp2PxUtil.dip2px(mContext, 1f)))
        }
        xloading_view.showLoading()
        presenter.getShopMessage(pageNumber, topMainTab.currentTab + 1)
    }

    override fun showMessageList(list: List<ShopMessageDto>) {
        if (pageNumber == 1) {
            mAdapter.setNewData(list)
        } else {
            mAdapter.addData(list)
        }
        if(mAdapter.mData.size == 0){
            xloading_view.showEmpty()
        }else{
            xloading_view.showContent()
        }
        onLoad(list.size)
    }

    private fun onLoad(size: Int) {
        if (ref_layout == null) {
            return
        }
        ref_layout.finishRefreshLoadingMore()
        if (size < 10) {
            ref_layout.enableLoadMore(false)
        } else {
            ref_layout.enableLoadMore(true)
        }
    }

    override fun initListener() {
        super.initListener()
        topMainTab.setOnTabSelectListener(object : OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                pageNumber = 1
                presenter.getShopMessage(pageNumber, position + 1)
            }
            override fun onTabReselect(position: Int) {
            }
        })
        ref_layout.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                pageNumber++
                presenter.getShopMessage(pageNumber, topMainTab.currentTab + 1)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                pageNumber = 1
                presenter.getShopMessage(pageNumber, topMainTab.currentTab + 1)
            }
        })
        mAdapter.setOnItemClickListener { _, item, _ ->
            if(item.type == 1){
                WebViewActivity.open(mContext,item.title,item.content)
            }else{
                STaskDetailActivity.open(mContext,item.missionId,STaskDetailActivity.COMMENT_OPEN)
            }
        }
        xloading_view.setOnRetryClickListener {
            pageNumber = 1
            xloading_view.showLoading()
            presenter.getShopMessage(pageNumber, topMainTab.currentTab + 1)
        }
    }

    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String?, message: String?) {
        showErrorMsg(code, message)
        onLoad(-1)
        if(postCode == 1|| postCode == -10086){
            xloading_view.showError()
        }
    }
}
